package controller

import (
	"bluebell/dao/mysql"
	"bluebell/models"
	"bluebell/pkg/snowflake"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// CommentHandler
// @Tags PantheonApi
// @Summary 创建评论
// @Schemes
// @Description	Post 请求, 参数在body中
// @Accept json
// @Produce json
// @Param	Comment body  models.Comment true "评论信息"
// @Success 200 {string} json{"code","message","data"}
// @Router	/api/Pantheon/comment [post]
func CommentHandler(c *gin.Context){
	var comment models.Comment
	if err := c.BindJSON(&comment); err != nil {
		fmt.Println(err)
		ResponseError(c, CodeInvalidParams)
		return
	}
	// 生成评论ID
	commentID, err := snowflake.GetID()
	if err != nil {
		zap.L().Error("snowflake.GetID() failed", zap.Error(err))
		ResponseError(c, CodeServerBusy)
		return
	}
	// 获取作者ID，当前请求的UserID
	userID, err := getCurrentUserId(c)
	if err != nil {
		zap.L().Error("GetCurrentUserID() failed", zap.Error(err))
		ResponseError(c, CodeNotLogin)
		return
	}
	comment.CommentID = commentID
	comment.AuthorID = userID
	// 创建评论
	if err := mysql.CreateComment(&comment); err != nil {
		zap.L().Error("mysql.CreateComment(&comment) failed", zap.Error(err))
		ResponseError(c, CodeServerBusy)
		return
	}
	ResponseSuccess(c, nil)
}

// CommentListHandler
// @Tags PantheonApi
// @Summary 评论列表
// @Schemes
// @Description	Get 请求, 参数在url中
// @Accept json
// @Produce json
// @Param	postId query int true "帖子id"
// @Success 200 {string} json{"code","message","data"}
// @Router	/api/Pantheon/comments [get]
func CommentListHandler(c *gin.Context) {
	postId, ok := c.GetQueryArray("postId")
	if !ok {
		ResponseError(c, CodeInvalidParams)
		return
	}
	posts, err := mysql.GetCommentListById(postId)
	if err != nil {
		ResponseError(c, CodeServerBusy)
		return
	}
	ResponseSuccess(c, posts)
}